<template>
  <div class="content-outer rowFlex">
    <div class="preview columnFlex columnCenter ">
      <div class="topImage">
        <div class="signList rowFlex allCenter">
          <div
            v-for="(item,index) in signConfig"
            :key="index"
            class="signList-item"
          >
            <div class="signList-item-image columnFlex allCenter">
              <p class="score">+{{ item.free_coin }}</p>
              <svg-icon
                icon-class="score"
                class="image"
              />
            </div>
            <div class="signList-item-title">
              {{ item.times }} 天
            </div>
          </div>
        </div>
      </div>
      <div class="taskCenter">
        <div class="taskDetails">
          <div class="title rowFlex spaceBetween">
            <p class="text">任务列表</p>
            <p class="infoText">任务每日0点重置</p>
          </div>
          <div class="taskList">
            <div
              v-for="(item,index) in taskConfigList"
              :key="index"
            >
              <div
                v-if="item.status==1"
                class="taskList-item rowFlex spaceBetween allCenter"
              >
                <div class="left rowFlex columnCenter">
                  <svg-icon
                    :icon-class="item.icon_url"
                    class="taskList-item-image"
                  />
                  <div class="taskList-item-text">
                    <p class="titleText rowFlex columnCenter"> <span class="taskTitle hidden">{{ item.name }}</span> <svg-icon
                      icon-class="taskIcon"
                      class="taskIcon"
                    /> <span class="taskScore">+{{ item.coin }}</span></p>
                    <p class="taskInfo">{{ item.introduce }}</p>
                    <p class="taskPregress rowFlex columnCenter">
                      <span class="progress"></span>
                      <span class="num">0/{{ item.num }}</span>
                    </p>
                  </div>
                </div>
                <div class="taskList-item-btn rowFlex allCenter">去完成</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="rightContent">
      <div class="signConfig columnFlex flex1">
        <div class="baseInfoTitle rowFlex columnCenter"> <span>签到设置</span> <span class="warningText">签到7天一周期，断签需重新开始签到，签满7天一个周期结束后将进入下一个循环周期</span> </div>
        <div class="baseInfoContent">
          <div class="inputList rowFlex flexWarp">
            <div
              v-for="(item,index) in signConfig"
              :key="index"
              class="signConfig-item rowFlex columnCenter"
            >
              签到 {{ item.times }} 天，奖励
              <el-input
                v-model="item.free_coin"
                clearable
                oninput="value=value.replace(/^0|[^0-9]/g,'')"
                style="width: 100px;margin:8px;"
                maxlength="6"
                placeholder="请输入赠币"
                @change="signConfigChange(index,$event)"
              ></el-input>
              赠币
            </div>
          </div>
          <div class="saveBtn rowFlex">
            <el-button
              :loading="singloading"
              type="primary"
              @click="saveSignData"
            >保存</el-button>
          </div>
        </div>
      </div>
      <div class="taskConfig flex1">
        <div class="baseInfoTitle rowFlex columnCenter"> <span>任务配置</span> <span class="warningText">任务每日0点重置</span> </div>
        <div class="taskConfigList">
          <draggable
            :list="taskConfigList"
            group="article"
            class="dragArea"
            :v-bind="options"
          >
            <el-collapse
              v-for="(item,index) in taskConfigList"
              :key="index"
              style="margin-bottom:20px;"
            >
              <el-collapse-item>
                <template slot="title">
                  <div class="titleHeader">
                    <svg-icon
                      icon-class="drag2"
                      style="margin-right:10px;font-size:16px;"
                    />
                    <!-- <i class="el-icon-rank" style="font-size:18px;margin-right:10px;"></i> -->
                    <el-checkbox
                      v-model="item.status"
                      true-label="1"
                      false-label="0"
                    >{{ item.name }}</el-checkbox>
                  </div>
                </template>
                <div class="collapseContent">
                  <el-form
                    ref="ruleForm"
                    class="ruleForm"
                    :model="item"
                    label-position="top"
                    label-width="100px"
                    :rules="rules"
                  >
                    <el-form-item
                      label="任务名称"
                      prop="name"
                      :draggable="false"
                    >
                      <el-input
                        v-model="item.name"
                        class="taskName"
                        clearable
                        maxlength="15"
                      ></el-input>
                    </el-form-item>
                    <!-- 独有的属性 -->
                    <!-- 追剧 -->
                    <el-form-item
                      v-if="item.type==4"
                      label="新追剧"
                      prop="num"
                      class="input"
                    >
                      <el-input
                        v-model="item.num"
                        oninput="value=value.replace(/^0|[^0-9]/g,'')"
                        clearable
                        style="width:150px !important;margin-right:10px;"
                        maxlength="3"
                        @change="numChange(index,'num',$event)"
                      ></el-input>部
                    </el-form-item>
                    <!-- 看剧 -->
                    <el-form-item
                      v-else-if="item.type==5"
                      label="看"
                      prop="num"
                      class="input"
                    >
                      <el-input
                        v-model="item.num"
                        oninput="value=value.replace(/^0|[^0-9]/g,'')"
                        clearable
                        style="width:150px !important;margin-right:10px;"
                        maxlength="3"
                        @change="numChange(index,'num',$event)"
                      ></el-input>集付费集
                    </el-form-item>
                    <!-- 点赞 -->
                    <el-form-item
                      v-else-if="item.type==3"
                      label="点赞"
                      prop="num"
                      class="input"
                    >
                      <el-input
                        v-model="item.num"
                        oninput="value=value.replace(/^0|[^0-9]/g,'')"
                        clearable
                        style="width:150px !important;margin-right:10px;"
                        maxlength="3"
                        @change="numChange(index,'num',$event)"
                      ></el-input>次
                    </el-form-item>
                    <!-- 分享 -->
                    <el-form-item
                      v-else-if="item.type==7"
                      label="分享"
                      prop="num"
                      class="input"
                    >
                      <el-input
                        v-model="item.num"
                        oninput="value=value.replace(/^0|[^0-9]/g,'')"
                        clearable
                        style="width:150px !important;margin-right:10px;"
                        maxlength="3"
                        @change="numChange(index,'num',$event)"
                      ></el-input>次
                    </el-form-item>
                    <el-form-item
                      label="任务说明"
                      prop="introduce"
                    >
                      <el-input
                        v-model="item.introduce"
                        maxlength="30"
                        class="introduceText"
                        style="height: 50px;"
                        clearable
                      ></el-input>
                    </el-form-item>
                    <el-form-item
                      label="任务奖励"
                      prop="coin"
                    >
                      <el-input
                        v-model="item.coin"
                        maxlength="5"
                        class="input"
                        style="width:150px !important;margin-right:10px;"
                        oninput="value=value.replace(/^0|[^0-9]/g,'')"
                        clearable
                        @change="numChange(index,'coin',$event)"
                      ></el-input>
                      赠币
                    </el-form-item>
                    <!-- 看剧追剧 -->
                    <el-form-item
                      v-if="item.type==4 || item.type==5 || item.type==3"
                      label="点击跳转"
                      prop="url_type"
                    >
                      <el-radio-group v-model="item.url_type">
                        <div class="rowFlex columnCenter">
                          <el-radio label="1">追剧</el-radio>
                          <el-radio label="2">剧场首页</el-radio>
                          <div class="rowFlex columnCenter playlet">
                            <el-radio label="3">指定剧</el-radio>
                            <el-select
                              v-if="item.url_type==3"
                              v-model="item.playlet_id"
                              :disabled="(item.is_exist && item.is_exist === 1 ) ? true : false"
                              remote
                              filterable
                              clearable
                              placeholder="请选择短剧"
                              style="width: 150px;"
                            >
                              <el-option
                                v-for="item in optionsList.playletList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                              > </el-option>
                            </el-select>
                          </div>
                        </div>
                      </el-radio-group>
                    </el-form-item>
                  </el-form>
                </div>
              </el-collapse-item>
            </el-collapse>
          </draggable>
          <div
            class="saveBtn rowFlex"
            style="margin-top:20px;"
          >
            <el-button
              :loading="saveloading"
              type="primary"
              @click="saveConfigData"
            >保存</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import draggable from 'vuedraggable'
import { configInfoNew, setConfigNew, taskList, saveTaskConfig, setSort } from '@/api/active'
import { commonData } from '@/api/common'
import { mapState } from 'vuex'
export default {
  components: {
    draggable
  },
  data() {
    return {
      saveloading: false,
      singloading: false,
      signConfig: [
        { free_coin: '', times: 1 },
        { free_coin: '', times: 2 },
        { free_coin: '', times: 3 },
        { free_coin: '', times: 4 },
        { free_coin: '', times: 5 },
        { free_coin: '', times: 6 },
        { free_coin: '', times: 7 }
      ],
      signList: [],
      taskConfigList: [
        {
          name: '完成一笔充值',
          type: '1',
          coin: '',
          introduce: '完成一笔充值',
          num: 1,
          sort: 1,
          playlet_id: '',
          status: 0,
          icon_url: 'chongzhi',
          url_type: '1'
        },
        {
          name: '开启订阅',
          type: '2',
          coin: '',
          playlet_id: '',
          introduce: '开启订阅',
          num: 1,
          sort: 2,
          status: 0,
          icon_url: 'dingyue',
          url_type: '1'
        },
        {
          name: '点赞',
          type: '3',
          coin: '',
          introduce: '点赞',
          num: '',
          sort: 3,
          playlet_id: '',
          status: 0,
          icon_url: 'dianzan',
          url_type: '1'
        },
        {
          name: '追剧',
          type: '4',
          coin: '',
          introduce: '追剧',
          num: '',
          sort: 4,
          playlet_id: '',
          status: 0,
          icon_url: 'zhuijv',
          url_type: '1'
        },
        {
          name: '看剧',
          type: '5',
          coin: '',
          playlet_id: '',
          introduce: '看剧',
          num: '',
          sort: 4,
          status: 0,
          icon_url: 'kanjv',
          url_type: '1'
        },
        {
          name: '小程序加入桌面',
          type: '6',
          coin: '',
          playlet_id: '',
          introduce: '小程序加入桌面',
          num: 1,
          sort: 2,
          status: 0,
          icon_url: 'xiaochengxujiaruzhuomian',
          url_type: '1'
        },
        {
          name: '分享',
          type: '7',
          coin: '',
          playlet_id: '',
          introduce: '分享',
          num: '',
          sort: 4,
          status: 0,
          icon_url: 'fenxiang',
          url_type: '1'
        },
        {
          name: '加入抖音群',
          type: '8',
          coin: '',
          playlet_id: '',
          introduce: '加入抖音群',
          num: 1,
          sort: 4,
          status: 0,
          icon_url: 'jiarudouyinqun',
          url_type: '1'
        }
      ],
      options: {
        filter: ['.input']
      },
      optionsList: {
        playletList: []
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        socre: [{ required: true, message: '请输入任务奖励', trigger: 'blur' }],
        num: [{ required: true, message: '请输入任务数量', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入任务说明', trigger: 'blur' }],
        coin: [{ required: true, message: '请输入任务奖励', trigger: 'blur' }],
        url_type: [{ required: true, message: '请选择点击跳转方式', trigger: 'change' }]
      }
    }
  },
  async mounted() {
    await this.getMiniAppPlaylet()
    this.configInfoNew()
    this.reqeustTaskList()
    this.$AccessReport.report('zhangyu-task', [this.$store.state.user.routerItem.name, '运营管理', '任务签到'])
    if (this.routerItem.app_type == 1) {
      this.taskConfigList.pop()
      this.taskConfigList.pop()
    }
  },
  computed: {
    ...mapState('user', ['routerItem', 'mini_app_id', 'menu'])
  },
  methods: {
    signConfigChange(index, value) {
      console.log(value)
      this.$set(this.signConfig[index], 'free_coin', value)
    },
    async getMiniAppPlaylet() {
      const res = await commonData({
        type: 'miniAppPlaylet',
        mini_app_id: localStorage.mini_app_id
      })
      if (res.status_code == 1) {
        this.optionsList.playletList = res.data.data
      }
    },
    // 获取签到数据
    async configInfoNew() {
      const data = {
        mini_app_id: window.localStorage.getItem('mini_app_id')
      }
      const res = await configInfoNew(data)
      if (res.status_code === 1) {
        if (res.data.length > 0) {
          this.signConfig = res.data
        }
      }
    },
    numChange(index, attr, value) {
      this.$set(this.taskConfigList[index], attr, value)
    },
    async saveConfigData() {
      const list = this.taskConfigList.filter((item) => item.status == 1)
      const name = list.find((item) => item.name === '')
      const num = list.find((item) => item.num === '')
      const introduce = list.find((item) => item.introduce === '')
      const coin = list.find((item) => item.coin === '')
      const url_type = list.find((item) => item.url_type == 4 && Number(item.playlet_id) == 0)
      if (name) {
        this.$message.warning('请输入活动名称')
        return false
      }
      if (num) {
        this.$message.warning('请输入任务数量')
        return false
      }
      if (introduce) {
        this.$message.warning('请输入活动描述')
        return false
      }
      if (coin) {
        this.$message.warning('请输入活动奖励')
        return false
      }
      if (url_type) {
        this.$message.warning('请选择跳转短剧')
        return false
      }
      const data = {
        mini_app_id: window.localStorage.getItem('mini_app_id'),
        task_config: this.taskConfigList
      }
      this.saveloading = true
      saveTaskConfig(data).then((res) => {
        if (res.status_code == 1) {
          this.$message.success('保存成功')
          // this.setSort()
        }
        this.saveloading = false
      })
    },
    // 设置排序
    setSort() {
      const data = this.taskConfigList.map((item, index) => {
        return {
          id: item.id,
          sort: index + 1
        }
      })
      setSort({ list: data }).then((res) => {
        if (res.status_code == 1) {
          console.log(res)
        }
      })
    },
    // 保存签到数据
    async saveSignData() {
      const item = this.signConfig.find((item) => item.free_coin === '')
      if (item) {
        this.$message.warning('请输入奖励赠币')
        return false
      }
      const data = {
        mini_app_id: window.localStorage.getItem('mini_app_id'),
        config: this.signConfig
      }
      this.singloading = true
      setConfigNew(data).then((res) => {
        if (res.status_code === 1) {
          this.$message.success(res.msg)
        }
        this.singloading = false
      })
    },
    reqeustTaskList() {
      const data = {
        mini_app_id: window.localStorage.getItem('mini_app_id')
      }
      taskList(data).then((res) => {
        if (res.status_code === 1) {
          res.data.data.map((item) => {
            if (item.type == 1) {
              item.icon_url = 'chongzhi'
            } else if (item.type == 2) {
              item.icon_url = 'dingyue'
            } else if (item.type == 3) {
              item.icon_url = 'dianzan'
            } else if (item.type == 4) {
              item.icon_url = 'zhuijv'
            } else if (item.type == 5) {
              item.icon_url = 'kanjv'
            } else if (item.type == 6) {
              item.icon_url = 'xiaochengxujiaruzhuomian'
            } else if (item.type == 7) {
              item.icon_url = 'fenxiang'
            } else if (item.type == 8) {
              item.icon_url = 'jiarudouyinqun'
            }
            if (item.url_type == 3) {
              item.playlet_id = Number(item.playlet_id)
            }
            if (item.playlet_id !== 0) {
              const playlet_id = this.optionsList.playletList.find((items) => items.value === item.playlet_id)
              if (!playlet_id) {
                item.playlet_id = ''
              }
            }
          })
          if (res.data.data.length > 0) {
            this.taskConfigList = res.data.data
          }
        }
      })
    },
    playletSelect(item, value) {
      console.log(item, value)
      item.playlet_id = value
    }
  }
}
</script>

<style lang="scss" scoped>
.content-outer {
  width: 100%;
  padding: 20px;
  background: #fff;
  min-height: 100%;
  .preview {
    width: 375px;
    height: auto;
    margin-right: 20px;
    .topImage {
      width: 100%;
      height: 366px;
      background: url('https://wanxiaomeng-1255977238.cos.ap-shanghai.myqcloud.com/mxy/web/taskCenter.png') no-repeat;
      background-size: 100% auto;
      position: relative;
    }
    .signList {
      width: 100%;
      height: 90px;
      margin-top: 180px;
      .signList-item {
        width: 42px;
        height: auto;
        margin-left: 3px;
        margin-right: 3px;
        .signList-item-image {
          width: 42px;
          height: 52px;
          background: #fdf1e0;
          border-radius: 4px;
          .score {
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #ff5a02;
          }
          .image {
            width: 24px;
            height: 28px;
            border-radius: 4px;
          }
        }
        .signList-item-title {
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #666666;
          text-align: center;
          margin-top: 5px;
        }
      }
    }
    .taskCenter {
      width: 375px;
      height: 550px;
      background: #f5f7f9;
      margin-top: -40px;
      padding-top: 60px;
      padding: 14px;
      .taskDetails {
        width: 100%;
        height: auto;
        padding: 12px;
        background: #fff;
        border-radius: 4px;
      }
      .title {
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
      }
      .infoText {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
      }
    }
    .taskList {
      width: 100%;
      height: auto;
      background: #fff;
      .taskList-item {
        width: 100%;
        height: auto;
        padding: 10px 0;
        border-bottom: 1px solid #f5f7f9;
        .taskList-item-image {
          font-size: 14px;
          width: 40px;
          height: 40px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #333333;
          margin-right: 10px;
        }
        .taskList-item-text {
          .titleText {
            max-width: 220px;
            .taskTitle {
              font-size: 13px;
              font-family: PingFangSC-Medium, PingFang SC;
              font-weight: 500;
              color: #333333;
              max-width: 200px;
            }
            .taskIcon {
              font-size: 14px;
              margin: 0 6px;
            }
            .taskScore {
              font-size: 14px;
              font-family: PingFangSC-Semibold, PingFang SC;
              font-weight: 600;
              color: #ff5f00;
            }
          }
          .taskInfo {
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
            margin-top: 2px;
            max-width: 200px;
          }
          .taskPregress {
            .progress {
              width: 70px;
              height: 5px;
              background: #e5e9ec;
              border-radius: 3px;
            }
            .num {
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #999999;
              margin-left: 10px;
            }
          }
        }
        .taskList-item-btn {
          width: 56px;
          height: 25px;
          background: #ff5f00;
          border-radius: 13px;
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
        }
      }
    }
  }

  .rightContent {
  }

  .signConfig,
  .taskConfig {
    height: auto;
    background: #f7f8fa;
    padding: 0 20px;
    margin-top: 20px;
    .baseInfoTitle {
      width: 100%;
      height: 54px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2e323b;
      border-bottom: 1px solid #dfe4ed;
      .warningText {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ff4d4f;
        margin-left: 20px;
      }
    }
  }
  .signConfig {
    margin-top: 0;
  }
  .baseInfoContent {
    width: 100%;
    height: auto;
    padding: 20px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    padding-left: 0;
    .signConfig-item {
      margin-right: 40px;
    }
  }
  .taskConfig {
    height: auto;
    ::v-deep .el-form--label-top .el-form-item__label {
      padding: 0;
    }
    ::v-deep .el-form-item--small .el-form-item {
      margin-bottom: 10px;
    }
    .taskConfigList {
      width: 100%;
      height: auto;
      padding: 20px 0;
      ::v-deep .el-collapse-item__header {
        height: 60px;
      }
      ::v-deep .el-collapse {
        border-left: 1px solid #e6ebf5;
        border-right: 1px solid #e6ebf5;
      }
      ::v-deep .el-input__inner,
      ::v-deep .el-textarea__inner,
      ::v-deep .el-input,
      ::v-deep .el-textarea {
        width: 400px;
      }
      .introduceText {
        ::v-deep .el-input__inner {
          height: 50px;
        }
      }

      .input {
        ::v-deep .el-input__inner,
        ::v-deep .el-input {
          width: 150px;
        }
      }
      .playlet {
        ::v-deep .el-input__inner,
        ::v-deep .el-input {
          width: 260px;
        }
      }
      .titleHeader {
        padding: 0 20px;
        color: #cdcdcd;
      }
      .collapseContent {
        width: 100%;
        height: auto;
        padding: 0 20px;
      }
    }
  }
  .dragArea {
  }
}
</style>